package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.TopData;
import com.itheima.ydd.entity.Article;
import com.itheima.ydd.entity.Cate;
import com.itheima.ydd.mapper.ArticleMapper;
import com.itheima.ydd.service.ArticleService;
import com.itheima.ydd.service.CateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private CateService cateService;

    @Override
    public HashMap<String, Object> getPageHashMap(String start_time, String end_time, String title, String cateid, Integer page, Integer limit) {
        Page<Article> articlePage = new Page<>(page, limit);
        LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
        lqw.eq(!"".equals(cateid), Article::getCateid, cateid).
                between(!start_time.equals("") || !end_time.equals(""), Article::getCreate_date, start_time, end_time).
                like(title != null, Article::getTitle, title);
        this.page(articlePage, lqw);
        List<Article> records = articlePage.getRecords();
        List<Cate> list = cateService.list();
        records.forEach(item -> {
            String cateid1 = item.getCateid();
            list.forEach(it -> {
                if (it.getId().toString().equals(cateid1)) {
                    item.setCatename(it.getCatename());
                }
            });
        });
        Integer count = this.count(lqw);
        HashMap<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("currentPage", page);
        map.put("pageSize", limit);
        map.put("totalPages", articlePage.getTotal());
        map.put("data", records);
        return map;
    }

    @Override
    public TopData getTopData(Integer page, Integer limit) {
        TopData topData = new TopData();

        topData.setDesc("首页大列表");

        DateTimeFormatter dfDate = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        //24小时最新文章数量
        String endDay = LocalDateTime.now().format(dfDate);
        String startDay = LocalDateTime.now().minusDays(1L).format(dfDate);
        LambdaQueryWrapper<Article> lqw1 = new LambdaQueryWrapper<>();
        lqw1.between(Article::getCreate_date, startDay, endDay);
        topData.setNum_day(this.count(lqw1));

        //一周文章最新数量
        String endWeek = LocalDateTime.now().format(dfDate);
        String startWeek = LocalDateTime.now().minusDays(7L).format(dfDate);
        LambdaQueryWrapper<Article> lqw2 = new LambdaQueryWrapper<>();
        lqw1.between(Article::getCreate_date, startWeek, endWeek);
        topData.setNum_week(this.count(lqw2));

        //分页数据
        Page<Article> articlePage = new Page<>(page, limit);
        this.page(articlePage, new QueryWrapper<>());
        List<Article> records = articlePage.getRecords();
        int count = this.count();
        HashMap<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("currentPage", page);
        map.put("pageSize", limit);
        map.put("totalPages", articlePage.getTotal());
        map.put("data", records);
        topData.setList(map);

        return topData;
    }

    /**
     * 获取侧边最新文章
     *
     * @return
     */
    @Override
    public HashMap<String, Object> getHot() {
        List<Article> list = this.list(new LambdaQueryWrapper<Article>().
                orderByDesc(Article::getCreate_date).
                select(
                        Article::getId,
                        Article::getLovenum,
                        Article::getPic,
                        Article::getTitle
                ).last("limit 6"));
        List<Cate> list1 = cateService.list();
        list.forEach(item -> {
            String cateid1 = item.getCateid();
            list1.forEach(it -> {
                if (it.getId().toString().equals(cateid1)) {
                    item.setCatename(it.getCatename());
                }
            });
        });
        HashMap<String, Object> map = new HashMap<>();
        map.put("desc", "侧边最新文章");
        map.put("list", list);
        return map;
    }

    /**
     * 侧边推荐文章
     *
     * @return
     */
    @Override
    public HashMap<String, Object> getRecommend() {
        List<Article> list = this.list(new LambdaQueryWrapper<Article>().
                eq(Article::getIstop, 1).
                orderByDesc(Article::getCreate_date).
                select(
                        Article::getId,
                        Article::getLovenum,
                        Article::getPic,
                        Article::getTitle
                ).last("limit 6"));
        List<Cate> list1 = cateService.list();
        list.forEach(item -> {
            String cateid1 = item.getCateid();
            list1.forEach(it -> {
                if (it.getId().toString().equals(cateid1)) {
                    item.setCatename(it.getCatename());
                }
            });
        });
        HashMap<String, Object> map = new HashMap<>();
        map.put("desc", "侧边推荐文章");
        map.put("list", list);
        return map;
    }

    /***
     * Desc:返回文章详情数据
     * @param id
     * @return {@link HashMap< String, Object>}
     * @author Cr Mr
     * @date 2022/3/3 13:28
     */
    @Override
    public HashMap<String, Object> show(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        //上一篇数据
        Article article1 = this.getById(id - 1);
        map.put("prev", new ArrayList<Article>().add(article1));

        //下一篇数据
        Article article2 = this.getById(id + 1);
        map.put("next", new ArrayList<Article>().add(article2));


        Article article = this.getById(id);
        map.put("info", article);

        List<Article> list = this.list(new LambdaQueryWrapper<Article>().
                eq(Article::getIstop, 1).
                orderByDesc(Article::getCreate_date).
                select(
                        Article::getId,
                        Article::getLovenum,
                        Article::getPic,
                        Article::getTitle
                ).last("limit 6"));
        List<Cate> list1 = cateService.list();
        list.forEach(item -> {
            String cateid1 = item.getCateid();
            list1.forEach(it -> {
                if (it.getId().toString().equals(cateid1)) {
                    item.setCatename(it.getCatename());
                }
            });
        });

        map.put("recommend", list);
        return map;
    }


}
